package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.codeless.bo.DataFormDisplayRuleBO;
import com.slipper.util.Tool;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormDisplayRuleDao;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class DataFormDisplayRuleService {
    @Resource
    private DataFormDisplayRuleDao dataFormDisplayRuleDao;
    
    public DataFormDisplayRuleBO queryBy(String id) {
        return dataFormDisplayRuleDao.queryBy(id);
    }
    
    public List<DataFormDisplayRuleBO> queryList(DataFormDisplayRuleBO bo) {
        return dataFormDisplayRuleDao.queryList(bo);
    }
    
    public PageInfo<DataFormDisplayRuleBO> queryPage(DataFormDisplayRuleBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormDisplayRuleDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(DataFormDisplayRuleBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return dataFormDisplayRuleDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return dataFormDisplayRuleDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String displayId, List<DataFormDisplayRuleBO> list, String employeeId) {
        if (Tool.isBlank(displayId)) {
            return 0;
        }

        int i = 0;
        for (DataFormDisplayRuleBO field : list) {
            field.setDisplayId(displayId);
            field.setOrdinal(++i);
            if (Tool.isBlank(field.getId())) {
                field.setId(UUID.getUUID());
                field.setUpdateId(employeeId);
                field.setCreateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                field.setCreateTime(System.currentTimeMillis());
                dataFormDisplayRuleDao.insert(field);
            } else {
                field.setUpdateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                dataFormDisplayRuleDao.update(field);
            }
        }

        // 删除被删除的数据
        DataFormDisplayRuleBO qo = new DataFormDisplayRuleBO();
        qo.setDisplayId(displayId);
        dataFormDisplayRuleDao.queryList(qo).stream()
                .filter(field -> list.stream().noneMatch(temp -> field.getId().equals(temp.getId())))
                .forEach(field -> dataFormDisplayRuleDao.delete(field.getId()));

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormDisplayRuleBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormDisplayRuleDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormDisplayRuleDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormDisplayRuleDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void deleteByDisplayId(String displayId) {
        if (Tool.isBlank(displayId)) {
            return;
        }

        dataFormDisplayRuleDao.deleteByDisplayId(displayId);
    }

    @WriteTransactional
    public void copyByDisplayId(String oldDisplayId, String newDisplayId, String employeeId) {
        if (Tool.isBlank(oldDisplayId) || Tool.isBlank(newDisplayId)) {
            return;
        }

        DataFormDisplayRuleBO qo = new DataFormDisplayRuleBO();
        qo.setDisplayId(oldDisplayId);
        dataFormDisplayRuleDao.queryList(qo).forEach(field -> {
            field.setId(UUID.getUUID());
            field.setDisplayId(newDisplayId);
            field.setUpdateId(employeeId);
            field.setCreateId(employeeId);
            dataFormDisplayRuleDao.insert(field);
        });
    }

    public List<DataFormDisplayRuleBO> queryByDisplayId(String displayId) {
        if (Tool.isBlank(displayId)) {
            return Collections.emptyList();
        }

        DataFormDisplayRuleBO qo = new DataFormDisplayRuleBO();
        qo.setDisplayId(displayId);
        return dataFormDisplayRuleDao.queryList(qo);
    }
    
    @WriteTransactional
    public int delete(String id) {
        return dataFormDisplayRuleDao.delete(id);
    }
}
